package pers.lan.stack;

import java.util.Stack;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/6 10:19 AM
 */
public class LC_856 {

    public static void main(String[] args) {
        System.out.println(
                new LC_856()
                        .scoreOfParentheses(
                                "()()"
                        )
        );
    }

    public int scoreOfParentheses(String S) {
        char[] chars = S.toCharArray();
        Stack<String> stack = new Stack<>();
        for (int i = chars.length - 1; i >= 0; i--) {
            if (chars[i] == '(') {
                int sum = 0;
                while (!stack.peek().equals(")")) {
                    sum += Integer.parseInt(stack.pop());
                }
                stack.pop();
                stack.push(String.valueOf(sum == 0 ? 1 : sum * 2));
            } else {
                stack.push(String.valueOf(chars[i]));
            }
        }
        int sum = 0;
        while (!stack.empty()) {
            sum += Integer.parseInt(stack.pop());
        }
        return sum;
    }
}
